package site.luojie.business.email.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import site.luojie.common.dto.SendVerifyCodeDto;
import site.luojie.common.mq.input.MessageVerifyCodeInput;
import site.luojie.common.mq.output.MessageVerifyCodeOutput;

import java.util.Date;

/**
 * @author jie.luo
 * @since 2020/9/16
 */
@EnableBinding({
        MessageVerifyCodeInput.class
})
public class MessageConsumerHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageConsumerHandler.class);

    @Autowired
    private JavaMailSender javaMailSender;

    @StreamListener(MessageVerifyCodeInput.VERIFY_CODE_INPUT)
    public void sendVerifyCode(Message<SendVerifyCodeDto> mqMessage) {
        LOGGER.info("message ：{}", mqMessage);
        SendVerifyCodeDto payload = mqMessage.getPayload();

        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject("这是一封测试邮件");
        // 设置邮件发送者
        message.setFrom("190642964@qq.com");
        // 设置邮件接收者，可以有多个接收者
        message.setTo(payload.getEmail());
        // 设置邮件抄送人，可以有多个抄送人
//        message.setCc("37xxxxx37@qq.com");
        // 设置隐秘抄送人，可以有多个
//        message.setBcc("14xxxxx098@qq.com");
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        message.setText("这是的验证码：" + payload.getVerifyCode());
        javaMailSender.send(message);
    }
}
